#import "ALHUDPopUpButtonCell.h"


#pragma mark Constants

static const CGFloat ALHUDPopUpCornerRadius = 3.0f;


#pragma mark -
#pragma mark Class Globals

static NSColor *ALHUDPopUpBorderColor;
static NSGradient *ALHUDPopUpBackgroundGradient;
static NSGradient *ALHUDPopUpBackgroundGradientPressed;


#pragma mark -
@interface ALHUDPopUpButtonCell (ALHUDPopUpButtonCellPrivateMethods)
-(NSBezierPath *)framePathWithRect_:(NSRect)frameRect;
@end


#pragma mark -
@implementation ALHUDPopUpButtonCell

+(void)initialize {
	ALHUDPopUpBorderColor = [[NSColor colorWithCalibratedWhite:0.75f alpha:1.0f] retain];
	NSColor *topColor = [NSColor colorWithCalibratedWhite:0.55 alpha:1.0f];
	NSColor *bottomColor = [NSColor colorWithCalibratedWhite:0.4 alpha:1.0f];
	ALHUDPopUpBackgroundGradient = [[NSGradient alloc] initWithStartingColor:topColor endingColor:bottomColor];
	NSColor *pressedTop = [NSColor colorWithCalibratedWhite:0.45 alpha:1.0];
	NSColor *pressedBottom = [NSColor colorWithCalibratedWhite:0.3 alpha:1.0];
	ALHUDPopUpBackgroundGradientPressed = [[NSGradient alloc] initWithStartingColor:pressedTop endingColor:pressedBottom];
}

-(NSBackgroundStyle)backgroundStyle {
	return NSBackgroundStyleDark;
}

-(NSBackgroundStyle)interiorBackgroundStyle {
	return NSBackgroundStyleDark;
}

-(void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView {
	NSRect borderRect = NSInsetRect(frame, 0.5f, 0.5f);
	NSBezierPath *borderPath = [self framePathWithRect_:borderRect];
	NSGradient *backgroundGradient = [self isHighlighted] ? ALHUDPopUpBackgroundGradientPressed : ALHUDPopUpBackgroundGradient;
	[backgroundGradient drawInBezierPath:borderPath angle:90.0f];
	[ALHUDPopUpBorderColor setStroke];
	[borderPath stroke];
	
	if ([self showsFirstResponder] && [self focusRingType] != NSFocusRingTypeNone) {
		NSBezierPath *focusPath = [NSBezierPath bezierPathWithRect:frame];
		[NSGraphicsContext saveGraphicsState];
		NSSetFocusRingStyle(NSFocusRingOnly);
		[focusPath fill];
		[NSGraphicsContext restoreGraphicsState];		
	}
}

-(NSBezierPath *)framePathWithRect_:(NSRect)frameRect {
	return [NSBezierPath bezierPathWithRoundedRect:frameRect xRadius:ALHUDPopUpCornerRadius yRadius:ALHUDPopUpCornerRadius];
}

@end
